package com.AAA.controller.scl;

import com.AAA.Util.EasyExcelUtil;
import com.AAA.entity.LayData;
import com.AAA.entity.MyPage;
import com.AAA.entity.mht.TMht;
import com.AAA.entity.scl.TScl;
import com.AAA.entity.sds.TSdsRecordjieguo;
import com.AAA.service.Impl.mht.TmhtRecordDedtailServiceImpl;
import com.AAA.service.Impl.scl.TSclRecordDedtailServiceImpl;
import com.AAA.service.scl.ITsclService;
import com.AAA.service.Impl.scl.TsclServiceimpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("tscl")
public class TSclController {
    @Autowired
    private ITsclService service;
    //批量添加
    @Autowired
    private TsclServiceimpl serviceimpl;
    @Autowired
    private TSclRecordDedtailServiceImpl  dedtailService;
    @PostMapping("addall")
    @ResponseBody
    public LayData addAll(HttpServletRequest request, Model model, MultipartFile file) throws IOException {
        List<Object> objects = EasyExcelUtil.readExcel(file, new TScl(), 1, 1);
        int count=0;
        List<Integer> list=new ArrayList<>();
        LayData layData=new LayData();
        for (Object t:objects){
            TScl tScl=(TScl) t;
            count++;
            Integer addall = serviceimpl.addall(tScl);
            if (addall != null){
                list.add(count);
            }
        }
        if (list.size()==0){
            layData.setMsg("添加成功");
        }else {
            layData.setData("有第"+list.toString()+"条未添加成功");
        }
        return layData;
    }

    //添加 and 修改
    @PostMapping("add")
    @ResponseBody
    public LayData add(TScl tScl){
        if (tScl.getQid()==null){
            return new LayData(service.add(tScl));
        }else {
            return new LayData(service.edit(tScl));
        }
    }

    //删除
    @PostMapping("del")
    @ResponseBody
    public LayData delete(Integer qid){
        TScl tScl = service.selectById(qid);
        Integer tid = tScl.getTid();
        boolean b = dedtailService.selectByTid(tid);
        if(b){
            return new LayData(0);
        }else {
            boolean delete = service.delete(qid);
            return new LayData(delete);
        }
    }

    //分页查询
    @RequestMapping("selectfy")
    @ResponseBody
    public  LayData selectfy(MyPage myPage, TScl tScl){
        return  new LayData(service.selectfy(myPage,tScl));
    }

    @GetMapping("index")
    public String getIndexAllSub(MyPage myPage, HttpServletRequest request){
        myPage.setLimit(90);
        myPage.setPage(1);
        PageInfo all = service.getAll(myPage);
        request.setAttribute("allsub",all);

        return "/scl/index";

    }
}
